<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="130px">
    <el-form-item label="合作方名称" prop="partnerName">
      <el-input v-model="dataForm.partnerName" placeholder="请输入合作方名称"></el-input>
    </el-form-item>
    <el-form-item label="合作方编码" prop="partnerCode">
      <el-input v-model="dataForm.partnerCode" placeholder="请输入合作方编码"></el-input>
    </el-form-item>
    <el-form-item label="合作方账号" prop="appId">
      <el-input v-model="dataForm.appId" placeholder="请输入合作方账号"></el-input>
    </el-form-item>
    <el-form-item label="合作方密码" prop="appSecret">
      <el-input v-model="dataForm.appSecret" placeholder="请输入合作方密码"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="state">
      <el-switch v-model="dataForm.state" @change="changeState"></el-switch>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          partnerId: 0,
          partnerName: '',
          partnerCode: '',
          appId: '',
          appSecret: '',
          loginTime: '',
          createTime: '',
          createPersonId: '',
          createPersonName: '',
          updateTime: '',
          updatePersonId: '',
          updatePersonName: '',
          deleteTime: '',
          deletePersonId: '',
          deletePersonName: '',
          state: true,
          deleteFlag: ''
        },
        dataRule: {
          partnerName: [
            { required: true, message: '合作方名称不能为空', trigger: 'blur' }
          ],
          partnerCode: [
            { required: true, message: '合作方编码不能为空', trigger: 'blur' }
          ],
          appId: [
            { required: true, message: '合作方账号不能为空', trigger: 'blur' }
          ],
          appSecret: [
            { required: true, message: '合作方密码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.partnerId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.partnerId) {
            this.$http({
              url: this.$http.adornUrl(`/partner/partnerinfo/info/${this.dataForm.partnerId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.partnerName = data.partnerInfo.partnerName
                this.dataForm.partnerCode = data.partnerInfo.partnerCode
                this.dataForm.appId = data.partnerInfo.appId
                this.dataForm.appSecret = data.partnerInfo.appSecret
                this.dataForm.state = data.partnerInfo.state == '0'
              }
            })
          }
        })
      },
      changeState: function (state) {
        this.dataForm.state = state
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/partner/partnerinfo/${!this.dataForm.partnerId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'partnerId': this.dataForm.partnerId || undefined,
                'partnerName': this.dataForm.partnerName,
                'partnerCode': this.dataForm.partnerCode,
                'appId': this.dataForm.appId,
                'appSecret': this.dataForm.appSecret,
                'state': this.dataForm.state == true ? '0' : '1'
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
